package com.badibadi.mytools;

import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.badibadi.uniclubber.R;

/**
 * 
 * 
 * 注意，volley库只适用于访问网络频繁，但是数据量不大的图片异步任务，比如下载文件，如果数据量过大的就很糟糕 下载文件用 Afinal框架
 * 
 * @author Administrator
 * 
 */
public class VolleyTools {

	/**
	 * Volley库加载网络图片
	 * 
	 * @param context
	 * @param imageUrl
	 * @param mImageView
	 */
	public static void displayImageByVolley(Context context, String imageUrl,
			ImageView mImageView) {
		// String imageUrl="http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";
		RequestQueue requestQueue = Volley.newRequestQueue(context);
		final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(
				20);
		ImageCache imageCache = new ImageCache() {
			@Override
			public void putBitmap(String key, Bitmap value) {
				lruCache.put(key, value);
			}

			@Override
			public Bitmap getBitmap(String key) {
				return lruCache.get(key);
			}
		};
		ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);
		ImageListener listener = ImageLoader.getImageListener(mImageView,
				R.drawable.noimage, R.drawable.noimage);
		imageLoader.get(imageUrl, listener);
	}

	/**
	 * 
	 * @param context
	 * @param imageUrl
	 *            url地址
	 * @param mNetworkImageView
	 *            NetworkImageView对象加载网络图片
	 */
	public static void displayImageByNetworkImageView(Context context,
			String imageUrl, NetworkImageView mNetworkImageView) {
		// String imageUrl="http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";
		RequestQueue requestQueue = Volley.newRequestQueue(context);
		final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(
				20);
		ImageCache imageCache = new ImageCache() {
			@Override
			public void putBitmap(String key, Bitmap value) {
				lruCache.put(key, value);
			}

			@Override
			public Bitmap getBitmap(String key) {
				return lruCache.get(key);
			}
		};
		try {
			ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);
			mNetworkImageView.setTag("url");
			mNetworkImageView.setImageUrl(imageUrl, imageLoader);
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	/**
	 * 利用Volley获取JSON数据
	 */
	public static void getJSONByVolley(Context context, String JSONDataUrl) {
		RequestQueue requestQueue = Volley.newRequestQueue(context);
		// String JSONDataUrl =
		// "http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json";
		final ProgressDialog progressDialog = ProgressDialog.show(context,
				"This is title", "...Loading...");

		JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
				Request.Method.GET, JSONDataUrl, null,
				new Response.Listener<JSONObject>() {
					@Override
					public void onResponse(JSONObject response) {
//						   ("response=" + response);
						if (progressDialog.isShowing()
								&& progressDialog != null) {
							progressDialog.dismiss();
						}
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError arg0) {
//						   ("sorry,Error");
					}
				});
		requestQueue.add(jsonObjectRequest);
	}
//	com.android.volley.toolbox.NetworkImageView
}
